/**
 * 
 */
package com.huawei.esdk.uc.sametime.widgets;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.huawei.esdk.uc.jna.UCService;
import com.huawei.esdk.uc.jna.Constants.AccountType;
import com.huawei.esdk.uc.jna.util.StringUtil;
import com.huawei.esdk.uc.sametime.ConstantsDefines;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.models.Participant;
import com.huawei.esdk.uc.sametime.models.ParticipantList;
import com.huawei.esdk.uc.sametime.services.UCCallService;
import com.huawei.esdk.uc.sametime.services.UCSettingsService;
import com.huawei.esdk.uc.sametime.util.ImageFactory;
import com.huawei.esdk.uc.sametime.util.PluginUtil;

/**
 * 工具栏上,拨号盘扩展按钮的控制类
 * @author y00175154
 * 
 */
public class DialPad implements IShellProvider
{

    private Shell dialPad;

    private Shell parent;

    private Text callNumberText;// 呼叫号码输入框

    private Button dialButton;// 呼叫按钮

    private Button delButton;// 删除按钮

    private Menu dialCallMenu;// 呼叫之后弹出的菜单
    
    private MenuItem voiceCallItem;
    
    private MenuItem videoCallItem;

    private boolean isRedial = false;

    public DialPad(Composite composite)
    {
        parent = composite.getShell();
    }

    public void dispose()
    {
        if (null != dialPad)
        {
            dialPad.dispose();
        }
    }

    @Override
    public Shell getShell()
    {
        if (null != dialPad)
        {
            dialPad.dispose();
        }
        dialPad = new Shell(parent, SWT.DIALOG_TRIM);
        dialPad.setText(Messages.DialPad);
        dialPad.setSize(225, 270);

        createContent(dialPad);

        return dialPad;
    }

    /**
     * Create the content.
     * @param parent
     * @param style
     */
    private void createContent(final Shell parent)
    {
        createCallNumberText(parent);
        createDialButton(parent);

        if (!isRedial)
        {
            createDialMenu(parent);
        }
        createDelButton(parent);
        createNumbersButton(parent);
    }

    private void createNumbersButton(final Shell parent)
    {
        Button numButton_1 = new Button(parent, SWT.NONE);
        numButton_1.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_1.setBounds(10, 76, 55, 35);
        numButton_1.setText("1");

        Button numButton_2 = new Button(parent, SWT.NONE);
        numButton_2.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_2.setText("2");
        numButton_2.setBounds(81, 76, 55, 35);

        Button numButton_3 = new Button(parent, SWT.NONE);
        numButton_3.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_3.setText("3");
        numButton_3.setBounds(151, 76, 55, 35);

        Button numButton_4 = new Button(parent, SWT.NONE);
        numButton_4.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_4.setText("4");
        numButton_4.setBounds(10, 117, 55, 35);

        Button numButton_5 = new Button(parent, SWT.NONE);
        numButton_5.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_5.setText("5");
        numButton_5.setBounds(81, 117, 55, 35);

        Button numButton_6 = new Button(parent, SWT.NONE);
        numButton_6.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_6.setText("6");
        numButton_6.setBounds(151, 117, 55, 35);

        Button numButton_7 = new Button(parent, SWT.NONE);
        numButton_7.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_7.setText("7");
        numButton_7.setBounds(10, 158, 55, 35);

        Button numButton_8 = new Button(parent, SWT.NONE);
        numButton_8.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_8.setText("8");
        numButton_8.setBounds(81, 158, 55, 35);

        Button numButton_9 = new Button(parent, SWT.NONE);
        numButton_9.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_9.setText("9");
        numButton_9.setBounds(151, 158, 55, 35);

        Button numButton_xing = new Button(parent, SWT.NONE);
        numButton_xing.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_xing.setText("*");
        numButton_xing.setBounds(10, 199, 55, 35);

        Button numButton_0 = new Button(parent, SWT.NONE);
        numButton_0.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_0.setText("0");
        numButton_0.setBounds(81, 199, 55, 35);

        Button numButton_jing = new Button(parent, SWT.NONE);
        numButton_jing.setFont(PluginUtil.getFontMicrosoftYahei());
        numButton_jing.setText("#");
        numButton_jing.setBounds(151, 199, 55, 35);

        addDialNumButtonSelectionListener(numButton_1, dialButton);
        addDialNumButtonSelectionListener(numButton_2, dialButton);
        addDialNumButtonSelectionListener(numButton_3, dialButton);
        addDialNumButtonSelectionListener(numButton_4, dialButton);
        addDialNumButtonSelectionListener(numButton_5, dialButton);
        addDialNumButtonSelectionListener(numButton_6, dialButton);
        addDialNumButtonSelectionListener(numButton_7, dialButton);
        addDialNumButtonSelectionListener(numButton_8, dialButton);
        addDialNumButtonSelectionListener(numButton_9, dialButton);
        addDialNumButtonSelectionListener(numButton_xing, dialButton);
        addDialNumButtonSelectionListener(numButton_0, dialButton);
        addDialNumButtonSelectionListener(numButton_jing, dialButton);
    }

    private void createDialButton(final Shell parent)
    {
        dialButton = new Button(parent, SWT.NONE);
        dialButton.setBounds(10, 43, 93, 27);
        dialButton.setImage(ImageFactory.loadImage(Display.getCurrent(),
                ImageFactory.ICON_DIAL_BTN));
        if (callNumberText.getText() == null
                || callNumberText.getText().equals(""))
        {
            dialButton.setEnabled(false);
        }
        // 为”拨号呼叫“按钮下拉菜单注册单击下拉箭头事件
        dialButton.addListener(SWT.Selection, new Listener()
        {
            @Override
            public void handleEvent(Event event)
            {
                if (!isRedial)
                {
                    // 如果触发的事件为单击按钮,获得"拨号呼叫"按钮所在坐标位置和大小，相对于父窗口
                    Rectangle rect = dialButton.getBounds();
                    Point pt = new Point(rect.x, rect.y + rect.height);// 计算菜单出现的起始位置
                    pt = parent.toDisplay(pt);// 将该点转化为于屏幕的相对位置
                    dialCallMenu.setLocation(pt.x, pt.y);// 设置菜单显示的位置
                    dialCallMenu.setVisible(true);// 显示菜单
                    if (ConstantsDefines.IP_PHONE_MODE.equals(UCSettingsService
                            .getPhoneJoinDev()))
                    {
                        videoCallItem.setEnabled(false);
                    }
                    else
                    {
                        videoCallItem.setEnabled(true);
                    }
                }

            }
        });
    }

    private void createDelButton(final Shell parent)
    {
        // 删除按钮
        delButton = new Button(parent, SWT.NONE);
        delButton.setImage(ImageFactory.loadImage(Display.getCurrent(),
                ImageFactory.ICON_DEL_BTN));
        delButton.setBounds(113, 43, 93, 27);
        delButton.setEnabled(false);
        addDelButtonSelectionListener(delButton, dialButton);
    }

    private void createDialMenu(final Shell parent)
    {
        // 定义一个弹出式菜单，使用常量SWT.POP_UP
        dialCallMenu = new Menu(parent, SWT.POP_UP);
        // 定义菜单中的菜单项
        voiceCallItem = new MenuItem(dialCallMenu, SWT.PUSH);
        voiceCallItem.setText(Messages.DialPad_VOIP_CALL);
        addDialBtnMenuItemSelectionListener(voiceCallItem, false);
        new MenuItem(dialCallMenu, SWT.SEPARATOR);
        videoCallItem = new MenuItem(dialCallMenu, SWT.PUSH);
        videoCallItem.setText(Messages.DialPad_VIDEO_CALL);
        addDialBtnMenuItemSelectionListener(videoCallItem, true);
    }

    private void createCallNumberText(final Shell parent)
    {
        callNumberText = new Text(parent, SWT.BORDER);
        callNumberText.setText("");
        callNumberText.setBounds(10, 10, 196, 25);
        callNumberText.addModifyListener(new ModifyListener()
        {

            @Override
            public void modifyText(ModifyEvent arg0)
            {
                int len = callNumberText.getText().length();
                if (len > 0)
                {
                    char lastChar = callNumberText.getText().charAt(len - 1);
                    // 如果不是合法的输入,则需要将最后一位去掉
                    if (!PluginUtil.isValidInput(lastChar))
                    {

                        if (1 == len)
                        {
                            callNumberText.setText("");
                        }
                        else if (2 == len)
                        {
                            String srcStr = callNumberText.getText();
                            callNumberText.setText(srcStr.substring(0, 1));
                            callNumberText.setSelection(1);
                        }
                        else
                        {
                            String newString = callNumberText.getText()
                                    .substring(0, len - 1);
                            callNumberText.setText(newString);
                            callNumberText.setSelection(newString.length());
                        }
                    }
                }
                if (callNumberText.getText().length() == 0)
                {
                    dialButton.setEnabled(false);
                    delButton.setEnabled(false);
                }
                else
                {
                    dialButton.setEnabled(true);
                    delButton.setEnabled(true);
                }

            }
        });
    }

    private void addDialNumButtonSelectionListener(final Button num_button,
            final Button dial_button)
    {
        num_button.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                String newStr = callNumberText.getText() + num_button.getText();
                callNumberText.setText(newStr);
                dial_button.setEnabled(true);
            }
        });
    }

    private void addDialBtnMenuItemSelectionListener(final MenuItem item,
            final boolean isVideo)
    {
        item.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                String dialed_num = callNumberText.getText();
                if (dialed_num.length() == 0)
                {
                    MessageDialog.openError(dialPad.getShell(),
                            Messages.ERROR_TIP,
                            Messages.DialPad_ERROR_CALL_NUMBER_EMPTY);
                }
                else
                {
                    if (ConstantsDefines.CallStatus.end != UCCallService
                            .getCallStatus())
                    {
                        MessageDialog.openError(dialPad.getShell(),
                                Messages.ERROR_TIP, Messages.IS_CALLING);
                    }
                    else
                    {
                        dispose();

                        Participant participant = new Participant();
                        ParticipantList participantList = new ParticipantList();

                        String ucAccount = UCService.getInstance()
                                .UC_SDK_GetUCAccount(dialed_num);
                        if (StringUtil.isEmptyStr(ucAccount))
                        {
                            ucAccount = dialed_num;
                            participant.setCallNumType(AccountType.UC_IPPHONE);
                        }
                        else
                        {
                            participant.setCallNumType(AccountType.UC_ACCOUNT);
                        }

                        participant.setUcAccount(ucAccount);
                        participant.setNumber(ucAccount);
                        participantList.addparticipants(participant);
                        try
                        {
                            EsdkUCConvWindow window = EsdkUCConvWindow
                                    .getInstance();
                            window.createUCConvWindow(participantList);
                            window.makeCall(isVideo);
                        }
                        catch (Exception ex)
                        {
                            ex.printStackTrace();
                        }
                    }

                }
            }
        });
    }

    private void addDelButtonSelectionListener(final Button del_button,
            final Button dial_button)
    {
        del_button.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                String dialStr = callNumberText.getText();
                String newDialStr = dialStr.substring(0, dialStr.length() - 1);
                callNumberText.setText(newDialStr);

                if (newDialStr.length() == 0)
                {
                    dial_button.setEnabled(false);
                    del_button.setEnabled(false);
                }
            }
        });
    }

    public void setRedialEnabled(boolean isRedial)
    {
        this.isRedial = isRedial;
    }
}
